﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using BusinessLayer.RegInfo;
using BusinessLayer.RegInfo.Entities;
using CommandLib;

namespace FindArtInfo
{
    /// <summary>
    /// Lớp thuộc tính check ủy quyền của người dùng. Kế thừa từ lớp AuthorizeAttribute
    /// </summary>
    public class CustomAuthorizeAttribute : AuthorizeAttribute
    {        
        /// <summary>
        /// Kiem tra dang nhap
        /// </summary>                

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var account = (Account)httpContext.Session[Constants.SessionKey.SESSION_USER_LOGIN];            
            return account != null;
        }
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            filterContext.HttpContext.Response.Redirect(string.Format("~/{0}/login.html", Utils.GetUrlByLanguage(CookiesStore.GetCookies(Constants.Cookie.COOKIE_LANGUAGE))));                                  
        }

    }

    public class CustomAuthorizePaidAttribute : AuthorizeAttribute
    {
        /// <summary>
        /// Kiem tra dang nhap
        /// </summary>                

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var account = (Account)httpContext.Session[Constants.SessionKey.SESSION_USER_LOGIN_PAID];
            //return true;
            return account != null;
        }
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            filterContext.HttpContext.Response.Redirect(string.Format("~/{0}/login-renew.html", Utils.GetUrlByLanguage(CookiesStore.GetCookies(Constants.Cookie.COOKIE_LANGUAGE))));
        }

    }

    public class CheckExpriesDateAttribute : AuthorizeAttribute
    {
        /// <summary>
        /// Kiem tra dang nhap
        /// </summary>                

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {            
            var accountTmp = (Account)httpContext.Session[Constants.SessionKey.SESSION_USER_LOGIN];
            if (accountTmp == null) return false;
            var account = new AccountBL().GetById(accountTmp.Id);
            DateTime dateTimeNow = new CommonBL().GetDateTimeNow();
            TimeSpan timeSpan = Convert.ToDateTime(account.EndDate) - dateTimeNow;
            if (timeSpan.Days < 0)
            {
                httpContext.Session[Constants.SessionKey.SESSION_USER_LOGIN_PAID] = account;
                httpContext.Session[Constants.SessionKey.SESSION_USER_LOGIN] = null;
                return false;
            }
            return true;
        }
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            if (filterContext.HttpContext.Session[Constants.SessionKey.SESSION_USER_LOGIN_PAID] != null)
            {
                filterContext.HttpContext.Response.Redirect(string.Format("~/{0}/renew.html", Utils.GetUrlByLanguage(CookiesStore.GetCookies(Constants.Cookie.COOKIE_LANGUAGE))));
            }
            else
            {
                filterContext.HttpContext.Response.Redirect(string.Format("~/{0}/register.html", Utils.GetUrlByLanguage(CookiesStore.GetCookies(Constants.Cookie.COOKIE_LANGUAGE))));
            }
        }

    }

    public class SetTempDataModelStateAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            filterContext.Controller.TempData["ModelState"] =
               filterContext.Controller.ViewData.ModelState;
        }
    }

    public class RestoreModelStateFromTempDataAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Controller.TempData.ContainsKey("ModelState"))
            {
                filterContext.Controller.ViewData.ModelState.Merge(
                    (ModelStateDictionary)filterContext.Controller.TempData["ModelState"]);
            }
        }
    }
}